<template>
  <div class="login_container">
    <!-- 表单区域 -->
    <div class="login_box">
      <!-- 头部 -->
      <div class="avarage_box">
        <img src="../assets/logo.png" alt="" />
      </div>
      <el-form class="login_form" :model="loginForm" :rules="loginFormRules" ref="loginFormRef">
        <!-- 用户名 -->
        <el-form-item prop="username">
          <el-input v-model="loginForm.username"></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input type="password" v-model="loginForm.password"></el-input>
        </el-form-item>
        <!-- 按钮 -->
        <el-form-item class="login_btns">
          <el-button type="primary" @click="doSubmit">登录</el-button>
          <el-button type="info" @click="doReset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
    data() {
        return {
            loginForm:{
                username:'admin',
                password:'123456'
            },
            loginFormRules:{
                username:[
                {required:true,message:'请输入用户名',trigger:'blur'},
                {min:3,max:10,message:'请输入长度在3到10之间个字符',trigger:'blur'}
                ],
                password:[
                {required:true,message:'请输入登录密码',trigger:'blur'},
                {min:6,max:10,message:'请输入长度在6到10之间个字符',trigger:'blur'}
                ]
            }
        }
    },
    methods: {
        doReset() {
            this.$refs.loginFormRef.resetFields();
        },
        doSubmit() {
            this.$refs.loginFormRef.validate(async valid => {
                if(!valid) return;
                // 校验通过发送请求
                const {data:res} = await this.$http.post('login',this.loginForm);
                // 通过status来判断登录成功与否
                // console.log(res);
                if(res.meta.status !== 200) return this.$message.error('登录失败');
                this.$message.success('登录成功');
                // 将token保存在sessionstorage
                window.sessionStorage.setItem('token',res.data.token);
                this.$router.push('/home');
            })
        }
    }
};
</script>

<style lang="scss" scoped>
$bgcolor: #2b4b6b;
.login_container {
  background-color: $bgcolor;
  height: 100vh;

  .login_box {
    width: 450px;
    height: 300px;
    background-color: #fff;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    border-radius: 3px;
    padding: 0 20px;
    box-sizing: border-box;
    display: flex;
    align-items: flex-end;

    .avarage_box {
      width: 130px;
      height: 130px;
      border-radius: 50%;
      padding: 10px;
      box-shadow: 0 0 10px #ddd;
      background-color: #fff;
      position: absolute;
      top: 0;
      left: 50%;
      transform: translate(-50%, -50%);

      img {
        background-color: #eee;
        width: 130px;
        height: 130px;
        border-radius: 50%;
      }
    }

    .login_form {
      width: 100%;
    }

    .login_btns {
      display: flex;
      justify-content: flex-end;
    }
  }
}
</style>